/* eslint-disable no-console */
import * as baidu from "baidu-aip-sdk";
import urllib from "urllib";
import formstream from "formstream";

class Baidu {
  constructor() {
    this.AppID = "16400774";
    this.APIKey = "CeOuAOvi7QZWPMXdjQWbPuP3";
    this.SecretKey = "gPLS7F91SB7AGIlQSunqufX70oKRiASm";
    this.data = {};
  }

  async getdata(name, x, option) {
    if (!this.data[name] || this.data[name].settime) {
      let data = await this[name](x, option);
      const value = new jisuan(data);
      this.data[name] = value.getdata();
    }
    return this.data[name].value;
  }

  async 人脸识别(image, option) {
    const AipFaceClient = baidu.face;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    option = {};
    option.face_field =
      "age,beauty,expression,faceshape,gender,glasses,race,facetype";
    option.face_type = "LIVE";
    option.max_face_num = "1";
    const image_type = "BASE64";
    try {
      const res = await client.detect(image, image_type, option);
      if (!res.error_code) {
        console.log(res);
        return res.result.face_list[0];
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 手势识别(image, option) {
    const AipFaceClient = baidu.bodyanalysis;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.gesture(image, option);
      if (!res.error_code) {
        console.log(res);
        return res.result[0];
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 人体关键点识别(image, option) {
    const AipFaceClient = baidu.bodyanalysis;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.bodyAnalysis(image, option);
      if (!res.error_code) {
        console.log(res);
        return res.person_info[0];
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 人体检测与属性识别(image, option) {
    const AipFaceClient = baidu.bodyanalysis;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.bodyAttr(image, option);
      if (!res.error_code) {
        console.log(res);
        return res.person_info[0];
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 人流量统计(image, option) {
    const AipFaceClient = baidu.bodyanalysis;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.bodyNum(image, option);
      if (!res.error_code) {
        console.log(res);
        return res;
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 人像分割(image, option) {
    const AipFaceClient = baidu.bodyanalysis;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.bodySeg(image, option);
      if (!res.error_code) {
        console.log(res);
        return res;
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 通用图像分析(image, option) {
    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.advancedGeneral(image, option);
      if (!res.error_code) {
        console.log(res);
        return res.result[0];
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 菜品识别(image, option) {
    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    option = {};
    option.top_num = "1";
    try {
      const res = await client.dishDetect(image, option);
      if (!res.error_code) {
        console.log(res);
        return res.result[0];
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 车型识别(image, option) {
    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    option = {};
    option.top_num = "1";
    try {
      const res = await client.carDetect(image, option);
      if (!res.error_code) {
        console.log(res);
        if (res.result.length) {
          return {
            color_result: res.color_result,
            name: res.result[0].name,
            year: res.result[0].year
          };
        }
        return {
          color_result: res.color_result
        };
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 商标识别(image, option) {
    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    option = {};
    option.top_num = "1";
    try {
      const res = await client.logoSearch(image, option);
      if (!res.error_code) {
        console.log(res);
        return res.result[0];
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 动物识别(image, option) {
    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    option = {};
    option.top_num = "1";
    try {
      const res = await client.animalDetect(image, option);
      if (!res.error_code) {
        console.log(res);
        return res.result[0];
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }
  async 植物识别(image, option) {
    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.plantDetect(image, option);
      if (!res.error_code) {
        console.log(res);
        return res.result[0];
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 花卉识别(image, option) {
    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.plantDetect(image, option);
      if (!res.error_code) {
        console.log(res);
        return res.result[0];
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 果蔬类食材(image, option) {
    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.plantDetect(image, option);
      if (!res.error_code) {
        console.log(res);
        return res.result[0];
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 地标识别(image, option) {
    const AipFaceClient = baidu.imageClassify;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.landmark(image, option);
      if (!res.error_code) {
        console.log(res);
        return res.result;
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 文字识别(image, option) {
    const AipFaceClient = baidu.ocr;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.generalBasic(image, option);

      if (res.words_result_num) {
        console.log(res);
        if (res.words_result.length) {
          let x = "";
          for (let v of res.words_result) {
            x += v.words + "\r";
          }
          return {
            words: x
          };
        }
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 网络图片文字识别(image, option) {
    const AipFaceClient = baidu.ocr;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.webImage(image, option);
      console.log(res);

      if (res.words_result_num) {
        if (res.words_result.length) {
          let x = "";
          for (let v of res.words_result) {
            x += v.words + "\r";
          }
          return {
            words: x
          };
        }
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 身份证识别(image, option = {}) {
    let idCardSide = option.idCardSide || "front";
    const AipFaceClient = baidu.ocr;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.idcard(image, idCardSide, option);

      if (res.words_result_num) {
        console.log(res);
        return res.words_result;
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 手写文字识别(image, option) {
    const AipFaceClient = baidu.ocr;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.handwriting(image, option);
      console.log(res);

      if (res.words_result_num) {
        return res.words_result[0];
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 名片识别(image, option) {
    let idCardSide = option.idCardSide || "front";
    const AipFaceClient = baidu.ocr;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.idcard(image, idCardSide, option);

      if (!res.error_code) {
        console.log(res);
        return res.words_result;
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }
  async 火车票识别(image, option) {
    let idCardSide = option.idCardSide || "front";
    const AipFaceClient = baidu.ocr;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.idcard(image, idCardSide, option);

      if (!res.error_code) {
        console.log(res);
        return res.words_result;
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async 数字识别(image, option) {
    const AipFaceClient = baidu.ocr;
    const client = new AipFaceClient(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.numbers(image, option);

      if (res.words_result_num) {
        console.log(res);
        return res.words_result[0];
      }
    } catch (err) {
      console.log(err);
    }
    return {};
  }

  async getspeek(buffer, dev_pid = 1537) {
    const client = new baidu.speech(this.AppID, this.APIKey, this.SecretKey);
    try {
      const res = await client.recognize(buffer, "pcm", 16000, {
        dev_pid: dev_pid
      });
      console.log(res);
      return res.result || [];
    } catch (err) {
      console.log(err);
    }
    return [];
  }

  async htebaidu(buffer, devpid) {
    const form = formstream();
    form.buffer("a.wav", buffer, "a.wav");
    form.field("devpid", devpid);
    try {
      const data = await urllib.request(
        "https://public.hterobot.com/security/baidu/sound2",
        {
          method: "POST",
          headers: form.headers(),
          stream: form,
          timeout: 10000
        }
      );
      let r = JSON.parse(data.data.toString());
      console.log(r);
      if (r.err_no == 0) {
        return r.result[0];
      }
    } catch (err) {
      console.log(err);
    }
    return "";
  }
}

class jisuan {
  constructor(data) {
    this.data = {
      value: data,
      settime: false
    };
    setTimeout(() => {
      this.data.settime = true;
    }, 1500);
  }

  getdata() {
    return this.data;
  }
}

export default Baidu;
